package com.ruoyi.ruoyixingyue.modules.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.page.PageData;
import com.ruoyi.ruoyixingyue.modules.api.entity.Course;
import com.ruoyi.ruoyixingyue.modules.api.entity.Media;
import com.ruoyi.ruoyixingyue.modules.api.entity.Order;
import com.ruoyi.ruoyixingyue.modules.api.entity.Student;
import com.ruoyi.ruoyixingyue.modules.api.service.IMediaService;
import com.ruoyi.ruoyixingyue.modules.api.vo.MediaListVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.MediaUpdateVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.OrderUpdateVo;
import com.ruoyi.ruoyixingyue.modules.utils.Condition;
import com.ruoyi.ruoyixingyue.modules.utils.QueryPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 视频文件表 前端控制器
 * </p>
 *
 * @author lzz
 * @since 2024-04-06
 */
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private IMediaService mediaService;

    @PostMapping("/media/list/API_001")
    public R getListAPI_001(@RequestBody Condition<MediaListVo> condition){
        QueryPage queryPage = condition.getPage();
        MediaListVo listVo = condition.getCondition();
        QueryWrapper<Media> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Objects.nonNull(listVo.getId()),Media::getId,listVo.getId())
                .eq(Objects.nonNull(listVo.getPlatform()),Media::getPlatform,listVo.getPlatform())
                .eq(Objects.nonNull(listVo.getStatus()),Media::getStatus,listVo.getStatus());
        List<Media> mediaList = mediaService.list(queryWrapper);
        mediaList.forEach(media -> {
            if (media.getStatus()==1){
                media.setStatusName("未上传");
            }else {
                media.setStatusName("已上传");
            }
        });
        Page<Media> resultPage = new Page<Media>().setCurrent(queryPage.getCurrent()).setSize(queryPage.getSize());
        Page<Media> page = mediaService.page(resultPage, queryWrapper);
        return R.ok(new PageData<>(mediaList,page.getTotal()));
    }

    @PostMapping("/media/getById/API_002/{id}")
    public R mediaGetByIdApi_002(@PathVariable("id") Long id){
        Media media = mediaService.getById(id);
        if (media.getStatus()==1){
            media.setStatusName("未上传");
        }else{
            media.setStatusName("已上传");
        }
        return R.ok(media);
    }

    @PostMapping("/media/update/API_003")
    public R mediaUpdateApi_003(@RequestBody MediaUpdateVo mediaUpdateVo){
        return R.ok("修改成功");
    }

    @PostMapping("/media/delete/API_004/{id}")
    public R mediaGetByIdApi_004(@PathVariable("id") Long id){
        boolean b = mediaService.removeById(id);
        if (b){
            return R.ok("删除成功");
        }else {
            return R.fail("删除失败");
        }
    }

}
